#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from google.appengine.ext import webapp
from google.appengine.ext import webapp
from google.appengine.api.urlfetch import fetch
from google.appengine.api import memcache
from google.appengine.ext.webapp import template
from BeautifulSoup import BeautifulSoup as Soup
from BeautifulSoup import SoupStrainer as Strainer
from BeautifulSoup import PageElement
from urllib import quote, unquote
from urlparse import urlparse, urlunparse, urljoin

import re

import site_plugin

import wsgiref.handlers
from google.appengine.ext import webapp


def readTemplate(file_path, *replacements):
  """Reads a file and returns its contents as a string."""
  file = open(file_path, 'rb')
  results = file.read()
  file.close()
  if len(replacements) > 1:
    results = results % replacements
  elif len(replacements) == 1:
    results = results % replacements[0]
  return results

class MainHandler(webapp.RequestHandler):
  def get(self):
    shemales = site_plugin.LumraxBase('shemale')
    page = shemales.calculatePageCount()
    
    for i in range(page - 5, page):
      links = shemales.produceLinks(i)
      self.response.out.write('<b>Page %s</b><br>' % i)
      for link in links:
        self.response.out.write('<a href="%s" style="margin:5px"><img src="%s" style="width:32px" border="0"></a>' %
            (link['href'], link['thumb']))
      self.response.out.write('<br><br>')
      
class iLumrax(webapp.RequestHandler):
  def get(self):
    categories = [('xxx', 'XXX'),('beauties', 'Beauties'),
        ('asian-beauties', 'Asian Beauties'),
        ('xmas-beauties', 'Christmas Beauties'), ('topless', 'Topless'),
        ('ratemywilly', 'Rate My Willy'), ('ratemypussy', 'Rate My Pussy'),
        ('nude', 'Nude'), ('chocolate-nude', 'Chocolate Nude'),
        ('asian-nude', 'Asian Nude'), ('hairy-nude', 'Hairy Nude'),
        ('shemale', 'Shemale'), ('redhead', 'Redhead'), ('milf', 'MILF'),
        ('big-boobs', 'Big Boobs'), ('the-butt-site', 'The Butt Site'),
        ('bj', 'Blowjob'), ('latinas', 'Latinas'),
        ('masturbating', 'Masturbating')]
        
    site = self.request.get('site')
    page = self.request.get('page')
    
    thumbnail = readTemplate('lumrax/lumrax_thumbnail.html')
    header = readTemplate('lumrax/lumrax_header.html', 
        (site, 'Menu')[site is ''])
    html = readTemplate('lumrax/lumrax_gallery.html')
    
    thumbnails = ""
    parser = site_plugin.LumraxBase(site)
    page_count = parser.calculatePageCount()
    
    if not page or int(page) > page_count:
      page = page_count
    
    links = parser.produceLinks(int(page))
    for link in links:
      thumbnails += thumbnail % link
    
    self.response.out.write(readTemplate('lumrax/lumrax_gallery.html') % 
        {'head': header, 'thumbnails': thumbnails})

class ClearGif(webapp.RequestHandler):
  def get(self):
    self.response.headers.add_header('content-type', 'image/gif')
    self.response.out.write(
        'GIF89a\x01\x00\x01\x00\x80\xff\x00\xc0'
        '\xc0\xc0\x00\x00\x00!\xf9\x04\x01\x00'
        '\x00\x00\x00,\x00\x00\x00\x00\x01\x00'
        '\x01\x00\x00\x02\x02D\x01\x00;')

def main():
  application = webapp.WSGIApplication([
    ('/', MainHandler),
    ('/imgs/clear.gif', ClearGif),
    ('/lumrax', iLumrax),
  ], debug=True)
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()
